﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.IDAL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class MasterPurchaseMappingDAL : IDALMasterPurchaseMapping
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasterPurchaseMappingDAL()
        {

        }

        public IList<MasterPurchaseMappingInfo> GetMasterPurchaseMapping()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterPurchaseMappingInfo> infoList = new List<MasterPurchaseMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT p.*"
                        + ", CASE WHEN p.AMOUNT_TYPE = '<' THEN CONCAT('ไม่เกิน ', CAST(FORMAT(p.MIN_AMOUNT,0) AS CHAR), ' บาท')"
                            + " WHEN p.AMOUNT_TYPE = '-' THEN CONCAT('ตั้งแต่ ', CAST(FORMAT(p.MIN_AMOUNT,0) AS CHAR), ' บาท ถึง ' , CAST(FORMAT(p.MAX_AMOUNT,0) AS CHAR), ' บาท')"
                            + " WHEN p.AMOUNT_TYPE = '>' THEN CONCAT('ตั้งแต่ ', CAST(FORMAT(p.MAX_AMOUNT,0) AS CHAR), ' บาทขึ้นไป ')"
                            + " ELSE 'ไม่กำหนด'"
                        + " END AS AMOUNT_SHOW"
                        + ", t.ICM_PC_TYPE_SEQ"
                        + ", t.ICM_PC_TYPE_NAME"
                        + ", m.ICM_PC_METHOD_NAME"
                        + ", r.ICM_PC_RULE_NAME"
                        + " FROM ICM_MAS_PURCHASE_MAPPING p"
                            + " INNER JOIN ICM_MAS_PURCHASE_METHOD m ON p.ICM_PC_METHOD_SEQ = m.ICM_PC_METHOD_SEQ"
                            + " INNER JOIN ICM_MAS_PURCHASE_TYPE t ON m.ICM_PC_TYPE_SEQ = t.ICM_PC_TYPE_SEQ"
                            + " INNER JOIN ICM_MAS_PURCHASE_RULE r ON p.ICM_PC_RULE_SEQ = r.ICM_PC_RULE_SEQ" 
                        + " WHERE p.RECORD_STATUS <> @delete_status";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterPurchaseMappingInfo info = new MasterPurchaseMappingInfo();
                        MasterPurchaseTypeInfo infoType = new MasterPurchaseTypeInfo();
                        MasterPurchaseMethodInfo infoMethod = new MasterPurchaseMethodInfo();
                        MasterPurchaseRuleInfo infoRule = new MasterPurchaseRuleInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_PC_MAP_SEQ"]);
                        info.Descr = (dr[i]["ICM_PC_MAP_DESCR"]).ToString();
                        infoMethod.Seq = (Convert.IsDBNull(dr[i]["ICM_PC_METHOD_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_PC_METHOD_SEQ"]));
                        infoMethod.Name = (dr[i]["ICM_PC_METHOD_NAME"]).ToString();
                        infoType.Seq = Convert.ToInt32(dr[i]["ICM_PC_TYPE_SEQ"]);
                        infoType.Name = (dr[i]["ICM_PC_TYPE_NAME"]).ToString();
                        infoMethod.InfoType = infoType;
                        infoRule.Seq = (Convert.IsDBNull(dr[i]["ICM_PC_RULE_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_PC_RULE_SEQ"]));
                        infoRule.Name = (dr[i]["ICM_PC_RULE_NAME"]).ToString();
                        info.Amount_type = (dr[i]["AMOUNT_TYPE"]).ToString();
                        info.Amount_show = (dr[i]["AMOUNT_SHOW"]).ToString();
                        info.Min_amount = (Convert.IsDBNull(dr[i]["MIN_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MIN_AMOUNT"]));
                        info.Max_amount = (Convert.IsDBNull(dr[i]["MAX_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MAX_AMOUNT"]));
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        info.InfoMethod = infoMethod;
                        info.InfoRule = infoRule;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<MasterPurchaseMappingInfo> GetMasterPurchaseMapping(string record_status)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterPurchaseMappingInfo> infoList = new List<MasterPurchaseMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@record_status", record_status);

                strSQL = "SELECT p.*"
                        + ", CASE WHEN p.AMOUNT_TYPE = '<' THEN CONCAT('ไม่เกิน ', CAST(FORMAT(p.MIN_AMOUNT,0) AS CHAR), ' บาท')"
                            + " WHEN p.AMOUNT_TYPE = '-' THEN CONCAT('ตั้งแต่ ', CAST(FORMAT(p.MIN_AMOUNT,0) AS CHAR), ' บาท ถึง ' , CAST(FORMAT(p.MAX_AMOUNT,0) AS CHAR), ' บาท')"
                            + " WHEN p.AMOUNT_TYPE = '>' THEN CONCAT('ตั้งแต่ ', CAST(FORMAT(p.MAX_AMOUNT,0) AS CHAR), ' บาทขึ้นไป ')"
                            + " ELSE 'ไม่กำหนด'"
                        + " END AS AMOUNT_SHOW"
                        + ", t.ICM_PC_TYPE_SEQ"
                        + ", t.ICM_PC_TYPE_NAME"
                        + ", m.ICM_PC_METHOD_NAME"
                        + ", r.ICM_PC_RULE_NAME"
                        + " FROM ICM_MAS_PURCHASE_MAPPING p"
                            + " INNER JOIN ICM_MAS_PURCHASE_METHOD m ON p.ICM_PC_METHOD_SEQ = m.ICM_PC_METHOD_SEQ"
                            + " INNER JOIN ICM_MAS_PURCHASE_TYPE t ON m.ICM_PC_TYPE_SEQ = t.ICM_PC_TYPE_SEQ"
                            + " INNER JOIN ICM_MAS_PURCHASE_RULE r ON p.ICM_PC_RULE_SEQ = r.ICM_PC_RULE_SEQ" 
                        + " WHERE p.RECORD_STATUS = @record_status";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterPurchaseMappingInfo info = new MasterPurchaseMappingInfo();
                        MasterPurchaseTypeInfo infoType = new MasterPurchaseTypeInfo();
                        MasterPurchaseMethodInfo infoMethod = new MasterPurchaseMethodInfo();
                        MasterPurchaseRuleInfo infoRule = new MasterPurchaseRuleInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_PC_MAP_SEQ"]);
                        info.Descr = (dr[i]["ICM_PC_MAP_DESCR"]).ToString();
                        infoMethod.Seq = (Convert.IsDBNull(dr[i]["ICM_PC_METHOD_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_PC_METHOD_SEQ"]));
                        infoMethod.Name = (dr[i]["ICM_PC_METHOD_NAME"]).ToString();
                        infoType.Seq = Convert.ToInt32(dr[i]["ICM_PC_TYPE_SEQ"]);
                        infoType.Name = (dr[i]["ICM_PC_TYPE_NAME"]).ToString();
                        infoMethod.InfoType = infoType;
                        infoRule.Seq = (Convert.IsDBNull(dr[i]["ICM_PC_RULE_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_PC_RULE_SEQ"]));
                        infoRule.Name = (dr[i]["ICM_PC_RULE_NAME"]).ToString();
                        info.Amount_type = (dr[i]["AMOUNT_TYPE"]).ToString();
                        info.Amount_show = (dr[i]["AMOUNT_SHOW"]).ToString();
                        info.Min_amount = (Convert.IsDBNull(dr[i]["MIN_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MIN_AMOUNT"]));
                        info.Max_amount = (Convert.IsDBNull(dr[i]["MAX_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MAX_AMOUNT"]));
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        info.InfoMethod = infoMethod;
                        info.InfoRule = infoRule;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public MasterPurchaseMappingInfo GetMasterPurchaseMapping(int seq)
        {
            string strSQL;
            DataSet ds;
            DataRowCollection dr;
            MasterPurchaseMappingInfo info = new MasterPurchaseMappingInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT p.*"
                        + ", CASE WHEN p.AMOUNT_TYPE = '<' THEN CONCAT('ไม่เกิน ', CAST(FORMAT(p.MIN_AMOUNT,0) AS CHAR), ' บาท')"
                            + " WHEN p.AMOUNT_TYPE = '-' THEN CONCAT('ตั้งแต่ ', CAST(FORMAT(p.MIN_AMOUNT,0) AS CHAR), ' บาท ถึง ' , CAST(FORMAT(p.MAX_AMOUNT,0) AS CHAR), ' บาท')"
                            + " WHEN p.AMOUNT_TYPE = '>' THEN CONCAT('ตั้งแต่ ', CAST(FORMAT(p.MAX_AMOUNT,0) AS CHAR), ' บาทขึ้นไป ')"
                            + " ELSE 'ไม่กำหนด'"
                        + " END AS AMOUNT_SHOW"
                        + ", t.ICM_PC_TYPE_SEQ"
                        + ", t.ICM_PC_TYPE_NAME"
                        + ", m.ICM_PC_METHOD_NAME"
                        + ", r.ICM_PC_RULE_NAME"
                        + " FROM ICM_MAS_PURCHASE_MAPPING p"
                            + " INNER JOIN ICM_MAS_PURCHASE_METHOD m ON p.ICM_PC_METHOD_SEQ = m.ICM_PC_METHOD_SEQ"
                            + " INNER JOIN ICM_MAS_PURCHASE_TYPE t ON m.ICM_PC_TYPE_SEQ = t.ICM_PC_TYPE_SEQ"
                            + " INNER JOIN ICM_MAS_PURCHASE_RULE r ON p.ICM_PC_RULE_SEQ = r.ICM_PC_RULE_SEQ" 
                        + " WHERE p.ICM_PC_MAP_SEQ = @seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    MasterPurchaseTypeInfo infoType = new MasterPurchaseTypeInfo();
                    MasterPurchaseMethodInfo infoMethod = new MasterPurchaseMethodInfo();
                    MasterPurchaseRuleInfo infoRule = new MasterPurchaseRuleInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                    info.Seq = Convert.ToInt32(dr[0]["ICM_PC_MAP_SEQ"]);
                    info.Descr = (dr[0]["ICM_PC_MAP_DESCR"]).ToString();
                    infoMethod.Seq = (Convert.IsDBNull(dr[0]["ICM_PC_METHOD_SEQ"]) ? null : (Nullable<int>)(dr[0]["ICM_PC_METHOD_SEQ"]));
                    infoMethod.Name = (dr[0]["ICM_PC_METHOD_NAME"]).ToString();
                    infoType.Seq = Convert.ToInt32(dr[0]["ICM_PC_TYPE_SEQ"]);
                    infoType.Name = (dr[0]["ICM_PC_TYPE_NAME"]).ToString();
                    infoMethod.InfoType = infoType;
                    infoRule.Seq = (Convert.IsDBNull(dr[0]["ICM_PC_RULE_SEQ"]) ? null : (Nullable<int>)(dr[0]["ICM_PC_RULE_SEQ"]));
                    infoRule.Name = (dr[0]["ICM_PC_RULE_NAME"]).ToString();
                    info.Amount_type = (dr[0]["AMOUNT_TYPE"]).ToString();
                    info.Amount_show = (dr[0]["AMOUNT_SHOW"]).ToString();
                    info.Min_amount = (Convert.IsDBNull(dr[0]["MIN_AMOUNT"]) ? null : (Nullable<double>)(dr[0]["MIN_AMOUNT"]));
                    info.Max_amount = (Convert.IsDBNull(dr[0]["MAX_AMOUNT"]) ? null : (Nullable<double>)(dr[0]["MAX_AMOUNT"]));
                    infoRecord.Created_by = (Convert.IsDBNull(dr[0]["CREATED_BY"]) ? null : (Nullable<int>)(dr[0]["CREATED_BY"]));
                    infoRecord.Created_when = (Convert.IsDBNull(dr[0]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["CREATED_WHEN"]));
                    infoRecord.Updated_by = (Convert.IsDBNull(dr[0]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[0]["UPDATED_BY"]));
                    infoRecord.Updated_when = (Convert.IsDBNull(dr[0]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["UPDATED_WHEN"]));
                    infoRecord.Restored_by = (Convert.IsDBNull(dr[0]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[0]["RESTORED_BY"]));
                    infoRecord.Restored_when = (Convert.IsDBNull(dr[0]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["RESTORED_WHEN"]));
                    infoStatus.Status = (dr[0]["RECORD_STATUS"]).ToString();
                    infoRecord.InfoStatus = infoStatus;
                    info.InfoMethod = infoMethod;
                    info.InfoRule = infoRule;
                    info.InfoRecord = infoRecord;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public IList<MasterPurchaseMappingInfo> GetMasterPurchaseMappingWithRule(int rule_seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterPurchaseMappingInfo> infoList = new List<MasterPurchaseMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@rule_seq", rule_seq);

                strSQL = "SELECT p.*"
                        + ", CASE WHEN p.AMOUNT_TYPE = '<' THEN CONCAT('ไม่เกิน ', CAST(FORMAT(p.MIN_AMOUNT,0) AS CHAR), ' บาท')"
                            + " WHEN p.AMOUNT_TYPE = '-' THEN CONCAT('ตั้งแต่ ', CAST(FORMAT(p.MIN_AMOUNT,0) AS CHAR), ' บาท ถึง ' , CAST(FORMAT(p.MAX_AMOUNT,0) AS CHAR), ' บาท')"
                            + " WHEN p.AMOUNT_TYPE = '>' THEN CONCAT('ตั้งแต่ ', CAST(FORMAT(p.MAX_AMOUNT,0) AS CHAR), ' บาทขึ้นไป ')"
                            + " ELSE 'ไม่กำหนด'"
                        + " END AS AMOUNT_SHOW"
                        + ", t.ICM_PC_TYPE_SEQ"
                        + ", t.ICM_PC_TYPE_NAME"
                        + ", m.ICM_PC_METHOD_NAME"
                        + ", r.ICM_PC_RULE_NAME"
                        + " FROM ICM_MAS_PURCHASE_MAPPING p"
                            + " INNER JOIN ICM_MAS_PURCHASE_METHOD m ON p.ICM_PC_METHOD_SEQ = m.ICM_PC_METHOD_SEQ"
                            + " INNER JOIN ICM_MAS_PURCHASE_TYPE t ON m.ICM_PC_TYPE_SEQ = t.ICM_PC_TYPE_SEQ"
                            + " INNER JOIN ICM_MAS_PURCHASE_RULE r ON p.ICM_PC_RULE_SEQ = r.ICM_PC_RULE_SEQ" 
                        + " WHERE p.RECORD_STATUS <> @delete_status"
                            + " AND r.ICM_PC_RULE_SEQ = @rule_seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterPurchaseMappingInfo info = new MasterPurchaseMappingInfo();
                        MasterPurchaseTypeInfo infoType = new MasterPurchaseTypeInfo();
                        MasterPurchaseMethodInfo infoMethod = new MasterPurchaseMethodInfo();
                        MasterPurchaseRuleInfo infoRule = new MasterPurchaseRuleInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_PC_MAP_SEQ"]);
                        info.Descr = (dr[i]["ICM_PC_MAP_DESCR"]).ToString();
                        infoMethod.Seq = (Convert.IsDBNull(dr[i]["ICM_PC_METHOD_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_PC_METHOD_SEQ"]));
                        infoMethod.Name = (dr[i]["ICM_PC_METHOD_NAME"]).ToString();
                        infoType.Seq = Convert.ToInt32(dr[i]["ICM_PC_TYPE_SEQ"]);
                        infoType.Name = (dr[i]["ICM_PC_TYPE_NAME"]).ToString();
                        infoMethod.InfoType = infoType;
                        infoRule.Seq = (Convert.IsDBNull(dr[i]["ICM_PC_RULE_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_PC_RULE_SEQ"]));
                        infoRule.Name = (dr[i]["ICM_PC_RULE_NAME"]).ToString();
                        info.Amount_type = (dr[i]["AMOUNT_TYPE"]).ToString();
                        info.Amount_show = (dr[i]["AMOUNT_SHOW"]).ToString();
                        info.Min_amount = (Convert.IsDBNull(dr[i]["MIN_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MIN_AMOUNT"]));
                        info.Max_amount = (Convert.IsDBNull(dr[i]["MAX_AMOUNT"]) ? null : (Nullable<double>)(dr[i]["MAX_AMOUNT"]));
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        info.InfoMethod = infoMethod;
                        info.InfoRule = infoRule;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<MasterPurchaseMappingInfo> GetMasterPurchaseMappingWithCondition(int type_seq, int rule_seq, double amount)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterPurchaseMappingInfo> infoList = new List<MasterPurchaseMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@type_seq", type_seq);
                dbManager.AddParameters(2, "@rule_seq", rule_seq);
                dbManager.AddParameters(3, "@amount", amount);

                strSQL = "SELECT p.ICM_PC_MAP_SEQ"
                        + ", m.ICM_PC_METHOD_NAME"
                        + " FROM ICM_MAS_PURCHASE_MAPPING p"
                            + " INNER JOIN ICM_MAS_PURCHASE_METHOD m ON p.ICM_PC_METHOD_SEQ = m.ICM_PC_METHOD_SEQ"
                        + " WHERE p.RECORD_STATUS <> @delete_status"
                            + " AND m.ICM_PC_TYPE_SEQ = @type_seq"
                            + " AND p.ICM_PC_RULE_SEQ = @rule_seq"
                            + " AND CASE WHEN p.AMOUNT_TYPE = '<' THEN @amount < p.MIN_AMOUNT"
                                + " WHEN p.AMOUNT_TYPE = '-' THEN @amount < p.MAX_AMOUNT  AND @amount >= p.MIN_AMOUNT"
                                + " WHEN p.AMOUNT_TYPE = '>' THEN @amount >= p.MAX_AMOUNT"
                                + " ELSE 0 = 0"
                            + " END";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterPurchaseMappingInfo info = new MasterPurchaseMappingInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_PC_MAP_SEQ"]);
                        info.Descr = (dr[i]["ICM_PC_METHOD_NAME"]).ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public void Insert(MasterPurchaseMappingInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@descr", info.Descr);
                dbManager.AddParameters(1, "@method_seq", info.InfoMethod.Seq);
                dbManager.AddParameters(2, "@rule_seq", info.InfoRule.Seq);
                dbManager.AddParameters(3, "@amount_type", info.Amount_type);
                dbManager.AddParameters(4, "@min_amount", info.Min_amount);
                dbManager.AddParameters(5, "@max_amount", info.Max_amount);
                dbManager.AddParameters(6, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(7, "@created_by", info.InfoRecord.Created_by);
                dbManager.AddParameters(8, "@created_when", info.InfoRecord.Created_when);

                strSQL = "INSERT INTO ICM_MAS_PURCHASE_MAPPING"
                    + " (ICM_PC_MAP_DESCR, ICM_PC_METHOD_SEQ, ICM_PC_RULE_SEQ, AMOUNT_TYPE, MIN_AMOUNT, MAX_AMOUNT"
                    + " , RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@descr"
                    + ", @method_seq"
                    + ", @rule_seq"
                    + ", @amount_type"
                    + ", @min_amount"
                    + ", @max_amount"
                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when"
                    + " )";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(MasterPurchaseMappingInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(10);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@descr", info.Descr);
                dbManager.AddParameters(2, "@method_seq", info.InfoMethod.Seq);
                dbManager.AddParameters(3, "@rule_seq", info.InfoRule.Seq);
                dbManager.AddParameters(4, "@amount_type", info.Amount_type);
                dbManager.AddParameters(5, "@min_amount", info.Min_amount);
                dbManager.AddParameters(6, "@max_amount", info.Max_amount);
                dbManager.AddParameters(7, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(8, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(9, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_PURCHASE_MAPPING"
                    + " SET ICM_PC_MAP_DESCR = @descr"
                    + ", ICM_PC_METHOD_SEQ = @method_seq"
                    + ", ICM_PC_RULE_SEQ = @rule_seq"
                    + ", AMOUNT_TYPE = @amount_type"
                    + ", MIN_AMOUNT = @min_amount"
                    + ", MAX_AMOUNT = @max_amount"
                    + ", RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_PC_MAP_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(MasterPurchaseMappingInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(3, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_PURCHASE_MAPPING"
                    + " SET RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_PC_MAP_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteAll(IList<MasterPurchaseMappingInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@updated_by", infoList[0].InfoRecord.Updated_by);
                dbManager.AddParameters(2, "@updated_when", infoList[0].InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_PURCHASE_MAPPING"
                    + " SET RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_PC_MAP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteFromDB(IList<MasterPurchaseMappingInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();

                strSQL = "DELETE FROM ICM_MAS_PURCHASE_MAPPING"
                    + " WHERE ICM_PC_MAP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Restore(IList<MasterPurchaseMappingInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(1, "@restored_by", infoList[0].InfoRecord.Restored_by);
                dbManager.AddParameters(2, "@restored_when", infoList[0].InfoRecord.Restored_when);

                strSQL = "UPDATE ICM_MAS_PURCHASE_MAPPING"
                    + " SET RECORD_STATUS = @record_status"
                    + ", RESTORED_BY = @restored_by"
                    + ", RESTORED_WHEN = @restored_when"
                    + " WHERE ICM_PC_MAP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
